﻿using Furion;
using Furion.Schedule;

using iMaoTai.Api.Core;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using SqlSugar;

using System;
using System.Text.Encodings.Web;

namespace iMaoTai.Api.Web.Core
{
    public class Startup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddConsoleFormatter();
            services.AddJwt<JwtHandler>();

            services.AddCorsAccessor();
            //sqlsugar单例注入
            services.AddSingleton<ISqlSugarClient>(DbContext.Instance);

            //内存缓存注入
            services.AddMemoryCache();
            //注册 远程请求服务
            services.AddRemoteRequest();
            //日志输出到文件
            services.AddFileLogging("logs/application-{0:yyyy}-{0:MM}-{0:dd}.log", options =>
            {
                options.FileNameRule = fileName =>
                {
                    return string.Format(fileName, DateTime.UtcNow);
                };
                options.Append = true;//追加
            });

            //services.AddStackExchangeRedisCache(options =>
            //{
            //    // 连接字符串，这里也可以读取配置文件
            //    options.Configuration = App.Configuration["RedisSetting:Reids_Url"];
            //    // 键名前缀
            //    options.InstanceName = App.Configuration["RedisSetting:Reids_Prefix"];
            //});

            //定时任务注入
            services.AddSchedule(options =>
            {
                //心跳
                options.AddJob<HeatbeatJob>("Heatbeat", Triggers.Minutely());
                //商品同步,1天一次 1点
                options.AddJob<ProductJob>("Product", Triggers.DailyAt(1));

                //门店同步,1天一次 2点
                options.AddJob<ShopJob>("Shop", Triggers.DailyAt(2));

                //批量预约,1天一次 9点05分
                options.AddJob<ReservationBatchJob>("ReservationBatch", Triggers.Cron("46 09 * * *"));
            });

            services.AddControllers()
                    .AddInjectWithUnifyResult()
                    .AddJsonOptions(options =>
                    {
                        options.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
                    });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseScheduleUI(options =>
            {
                options.RequestPath = "/myjob";
                //options.DisableOnProduction = false;
            });
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCorsAccessor();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseInject(string.Empty);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}